package com.herui.iot.ui.evaluation.execute;

import com.herui.device_lib.listener.DeviceLis;
import com.herui.sdyu_lib.http.callback.Callback;
import com.herui.sport_care.BasePresenter;
import com.herui.sport_care.BaseView;
import com.herui.sport_care.data.RepositoryFactory;
import com.herui.sport_care.data.check.BetaDataUploadForm;
import com.herui.sport_care.data.check.DeviceInfo;
import com.herui.sport_care.data.check.EquipmentType;
import com.herui.sport_care.data.check.EvalUserInfo;
import com.herui.sport_care.data.check.source.CheckSource;
import com.herui.sport_care.data.user.UserInfo;
import com.herui.sport_care.data.user.source.UserSource;

import java.util.List;

/**
 * Created by amanda.shan on 2022/12/12.
 */
public class ExecteServerPresenter implements BasePresenter {

    public static interface View extends BaseView {
        // 回调等待被测试者列表
        default void onEvalUserInfo(List<EvalUserInfo> evalUserInfos){}
        // 回调设备列表
        default void onDeviceInfo(List<DeviceInfo> deviceInfos){}
        // 回调上传疲劳成功
        default void onUploadRpeSucess(){}
    }

    CheckSource checkSource;
    View view;

    public ExecteServerPresenter(View view) {
        this.view = view;
        checkSource = RepositoryFactory.getInstance().getCheckSource();
    }

    @Override
    public void start() {

    }

    @Override
    public BaseView getView() {
        return view;
    }

    /**
     * 查询等待被测试者列表
     * @param evaluationSchemeId    评估方案Id
     * @param type                  评估进程type: 0 代表静息测量 ， 1 代表执行测试方案
     */
    public void findAwaitItemList(Long evaluationSchemeId, int type){

        UserSource userSource = RepositoryFactory.getInstance().getUserSource();
        UserInfo userInfo = userSource.getUserInfo();

        checkSource.findAwaitItemList(evaluationSchemeId, type, userInfo.getId(),
                new Callback.CommonCallback<List<EvalUserInfo>>() {
                    @Override
                    public void onSuccess(List<EvalUserInfo> result) {
                        view.onEvalUserInfo(result);
                    }

                    @Override
                    public void onError(Throwable e, String code, String message) {
                        processError(e, code, message);
                    }
                });
    }

    /**
     * 查询可用设备列表
     * @param evaluationSchemeId    评估方案id
     * @param userId            受评者用户id
     * @param equipmentType     设备类型
     */
    public void getAvailableEquipmentList(Long evaluationSchemeId, long userId,
                                          EquipmentType equipmentType){

        UserSource userSource = RepositoryFactory.getInstance().getUserSource();
        UserInfo userInfo = userSource.getUserInfo();
        long organId = userInfo.getOrganId();

        checkSource.getAvailableEquipmentList(evaluationSchemeId, organId, userId, equipmentType,
                new Callback.CommonCallback<List<DeviceInfo>>() {
                    @Override
                    public void onSuccess(List<DeviceInfo> result) {
                        view.onDeviceInfo(result);
                    }

                    @Override
                    public void onError(Throwable e, String code, String message) {
                        processError(e, code, message);
                    }
                });
    }

    /**
     * 绑定设备
     * @param evaluationSchemeId    评估方案id
     *  @param equipmentId          设备id
     * @param userId            受评者用户id
     * @param equipmentType     设备类型
     */
    public void equipmentUserSchemeBind(Long evaluationSchemeId, long equipmentId, long userId,
                                        EquipmentType equipmentType){

        UserSource userSource = RepositoryFactory.getInstance().getUserSource();
        UserInfo userInfo = userSource.getUserInfo();
        long organId = userInfo.getOrganId();

        checkSource.equipmentUserSchemeBind(evaluationSchemeId, equipmentId, userId, equipmentType,
                new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                    }

                    @Override
                    public void onError(Throwable e, String code, String message) {
                        processError(e, code, message);
                    }
                });
    }

    /**
     * 解绑定设备
     * @param evaluationSchemeId    评估方案id
     *  @param equipmentId          设备id
     * @param userId            受评者用户id
     */
    public void equipmentUserSchemeUnBind(Long evaluationSchemeId, long equipmentId, long userId){

        UserSource userSource = RepositoryFactory.getInstance().getUserSource();
        UserInfo userInfo = userSource.getUserInfo();
        long organId = userInfo.getOrganId();

        checkSource.equipmentUserSchemeUnBind(evaluationSchemeId, equipmentId, userId,
                new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                    }

                    @Override
                    public void onError(Throwable e, String code, String message) {
                        processError(e, code, message);
                    }
                });
    }

    public void dataUpload(BetaDataUploadForm betaDataUploadForm){
        UserSource userSource = RepositoryFactory.getInstance().getUserSource();
        UserInfo userInfo = userSource.getUserInfo();
        long organId = userInfo.getOrganId();

        // 被评估人
        betaDataUploadForm.setEvaluatorId(userInfo.getId());

        checkSource.dataUpload(betaDataUploadForm, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

            }

            @Override
            public void onError(Throwable e, String code, String message) {

            }
        });
    }

    /**
     *  自觉最大用力程度(疲劳程度)上传
     * @param betaDataId    评估数据id
     * @param rep           自觉最大用力描述
     */
    public void uploadRpe(Long betaDataId, String rep){
        checkSource.uploadRpe(betaDataId, rep, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                view.onUploadRpeSucess();
            }

            @Override
            public void onError(Throwable e, String code, String message) {
                processError(e, code, message);
            }
        });
    }
}
